import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminRegisterFrame extends JFrame {
    private JTextField accountField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField managerNameField;
    private JTextField contactNumberField;

    public AdminRegisterFrame() {
        setTitle("MBTI职业性格测试 - 管理员注册");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2));

        JLabel accountLabel = new JLabel("账号:");
        panel.add(accountLabel);

        accountField = new JTextField();
        panel.add(accountField);

        JLabel passwordLabel = new JLabel("密码:");
        panel.add(passwordLabel);

        passwordField = new JPasswordField();
        panel.add(passwordField);

        JLabel confirmPasswordLabel = new JLabel("确认密码:");
        panel.add(confirmPasswordLabel);

        confirmPasswordField = new JPasswordField(); // 初始化
        panel.add(confirmPasswordField);

        JLabel managerNameLabel = new JLabel("管理员姓名:");
        panel.add(managerNameLabel);

        managerNameField = new JTextField();
        panel.add(managerNameField);

        JLabel contactNumberLabel = new JLabel("联系电话:");
        panel.add(contactNumberLabel);

        contactNumberField = new JTextField();
        panel.add(contactNumberField);

        JButton registerButton = new JButton("注册");
        panel.add(registerButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = accountField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();
                String confirmPassword = new String(confirmPasswordField.getPassword()).trim(); // 获取确认密码
                String managerName = managerNameField.getText().trim();
                String contactNumber = contactNumberField.getText().trim();

                if (account.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || managerName.isEmpty() || contactNumber.isEmpty()) {
                    JOptionPane.showMessageDialog(AdminRegisterFrame.this, "所有字段均为必填项，请填写完整！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(AdminRegisterFrame.this, "两次输入的密码不一致，请重新输入！", "错误", JOptionPane.ERROR_MESSAGE);
                    passwordField.setText("");
                    confirmPasswordField.setText("");
                    return;
                }


                User user = new User();
                user.setAccount(account);
                user.setPIN(password);
                user.setUtype("manager");

                UserDAO userDAO = new UserDAOImpl();
                try {
                    userDAO.save(user);

                    Manager manager = new Manager();
                    manager.setAccount(account);
                    manager.setMname(managerName);
                    manager.setMnumber(contactNumber);

                    ManagerDAO managerDAO = new ManagerDAOImpl();
                    managerDAO.save(manager);

                    JOptionPane.showMessageDialog(AdminRegisterFrame.this, "管理员注册成功！");
                    dispose();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(AdminRegisterFrame.this, "注册出现错误，请稍后重试！", "错误", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });

        add(panel);
    }

    public void setConfirmPasswordField(JPasswordField confirmPasswordField) {
        this.confirmPasswordField = confirmPasswordField;
    }
}